home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / general / common_size.m < prev    next >
Text File  |  1996-11-20  |  2KB  |  71 lines

  1. ## Copyright (C) 1995, 1996  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage:  [errorcode, y_1, ...] = common_size (x_1, ...)
  18. ##
  19. ## Determine if all input arguments are either scalar or of common
  20. ## size.  In this case, errorcode is zero, and y_i is a matrix of the
  21. ## common size with all entries equal to x_i if this is a scalar or
  22. ## x_i otherwise.
  23. ##
  24. ## If the inputs cannot be brought to a common size, errorcode is 1, and
  25. ## y_i is x_i.
  26. ##
  27. ## For example,
  28. ##
  29. ##   [errorcode, a, b] = common_size ([1 2; 3 4], 5)
  30. ##
  31. ## returns errorcode = 0, a = [1 2, 3 4], b = [5 5; 5 5].
  32. ##
  33. ## This is useful for implementing functions where arguments can either
  34. ## be scalars or of common size.
  35.  
  36. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  37. ## Created: 15 October 1994
  38. ## Adapted-By: jwe
  39.  
  40. function [errorcode, ...] = common_size (...)
  41.   
  42.   if (nargin < 2)
  43.     error ("common_size: only makes sense if nargin >= 2");
  44.   endif
  45.  
  46.   va_start ();
  47.   for k = 1 : nargin
  48.     s(k, :) = size (va_arg ());
  49.   endfor
  50.   
  51.   m = max (s);
  52.   if (any (any ((s != 1)') & any ((s != ones (nargin, 1) * m)')))
  53.     errorcode = 1;
  54.     va_start ();
  55.     for k = 1 : nargin
  56.       vr_val (va_arg ());
  57.     endfor
  58.   else
  59.     errorcode = 0;
  60.     va_start ();
  61.     for k = 1 : nargin
  62.       if (prod (s(k, :)) == 1)
  63.     vr_val (va_arg () * ones (m));
  64.       else
  65.     vr_val (va_arg ());
  66.       endif
  67.     endfor
  68.   endif
  69.  
  70. endfunction
  71.